import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_wanandroid/common/api.dart';
import 'package:flutter_wanandroid/entity/navi_entity.dart';
import 'package:flutter_wanandroid/httpUtils/http_util.dart';
import 'package:flutter_wanandroid/res/colors.dart';

class NaviPage extends StatefulWidget {
  const NaviPage({Key? key}) : super(key: key);

  @override
  State<NaviPage> createState() => _NaviPageState();
}

class _NaviPageState extends State<NaviPage> {
  List<DataBean> _listData = [];
  ScrollController _scrollController = ScrollController();
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    getHttp();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Expanded(
          flex: 2,
          child: ListView.builder(
            controller: _scrollController,
            itemCount: _listData.length,
            itemBuilder: (context, i) {
              Color _color = Theme.of(context).primaryColor;
              return GestureDetector(
                child: Container(
                  padding: EdgeInsets.all(10),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: (_currentIndex == i)
                          ? YColors.color_F9F9F9
                          : Colors.white,
                      border: Border(
                          left: BorderSide(
                        width: 5,
                        color: (_currentIndex == i) ? _color : Colors.white,
                      ))),
                  child: Text(
                    _listData[i].name ?? "",
                    style: TextStyle(
                      fontSize: 16,
                      color: (_currentIndex == i)
                          ? YColors.colorPrimary
                          : YColors.color_666,
                      fontWeight: (_currentIndex == i)
                          ? FontWeight.bold
                          : FontWeight.normal,
                    ),
                  ),
                ),
                onTap: () {
                  setState(() {
                    _currentIndex = i;
                  });
                  print(i);
                },
              );
            },
          ),
        ),
        Expanded(
          flex: 5,
          child: ListView(
            children: [
              Container(
                alignment: Alignment.topLeft,
                padding: const EdgeInsets.all(10),
                color: YColors.color_F9F9F9,
                child: Wrap(
                  spacing: 10,
                  direction: Axis.horizontal,
                  alignment: WrapAlignment.start,
                  children: List<Widget>.generate(
                    (_listData.isNotEmpty &&
                            _listData[_currentIndex].articles != null)
                        ? _listData[_currentIndex].articles!.length
                        : 0,
                    (index) {
                      return ActionChip(
                        label: Text(
                          _listData[_currentIndex].articles?[index].title ?? "",
                          style: const TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 14,
                              color: YColors.color_666),
                        ),
                        onPressed: () {
                          print(index);
                        },
                      );
                    },
                  ).toList(),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }

  void getHttp() async {
    var response = await HttpUtil().get(Api.NAVI);
    print(response.toString());
    Map<String, dynamic> map = json.decode(response.toString());
    var fromMap = NaviEntity.fromMap(map) as NaviEntity;
    if (mounted) {
      setState(() {
        if (fromMap != null && fromMap.data != null) {
          _listData = fromMap.data!;
        }
      });
    }
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }
}
